<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\facade\Builder;
use think\facade\View;
use think\response\Json;
use think\facade\Db;
//该文件为自动生成，请按需修改
class Seckill extends Base
{
    //所有定义变量均为演示，请根据实际需求更改

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Seckill';



    public function index(): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '标题', 'modify' => '2,input'])
            ->addColumn(['field' => 'date_value', 'title' => '活动时间', 'modify' => '2,input'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['field' => 'status', 'title' => '是否开启活动', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 260])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete',
            'seckillAdd' => [
                'size' => '1300,1000',
                'title' => '添加秒杀商品',
                'url'   => url('seckillProduct/index')->build(),
                'event' => '_openNewPage'
            ]])
            ->addTopButtons(['add' => ['size' => '1000,1000'], 'deleteAll'])
            ->addSearch([
                ['title', '标题搜索', 'input', '', '', '', 'like']
            ])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭'],
            ])
            ->fetch();
    }
    // ',
    // 'href'   => url('seckill/add', ['tableUniqueStr' =>'admin_seckill_index','id'=>1])->build(),
    // 'event' => '_batchSubmit'
    public function add($tpl = 'seckill/edit')
    {
        return parent::add($tpl);
    }

    public function edit($id = '', $tpl = 'seckill/edit', $info = '')
    {
        $info = $this->model::find($id);
        return parent::edit($id, $tpl, $info);
    }

    public function update(): Json
    {
        
        try {
            $param = $this->request->post();
            $id=$param['id'];
            if($param['val'] == 'true'){
                $this->model::where([['id','>',0]])->save(array('status'=>0));
                $status=1;
            }else{
                $status=0;
            }
            $this->model::name('seckill')->where('id',$id)->save(array('status'=>$status));
            return json(['code' => 1, 'msg' => '操作成功']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function saveSeckill(): Json
    {
        return json($this->model::saveSeckill($this->request->post())); // TODO: Change the autogenerated stub
    }

    

}
